const app = getApp();

var type = 'add';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 'add',
    info: null,
    pics: [],
    card_id:''
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    if (options.type) {
      type = options.type;
      wx.setNavigationBarTitle({
        title: '编辑服务商品',
      })
      var url = 'https://www.58zltc.com/api.php/Card/getGoodAddForm/openid/' + app.globalData.openid + '/card_id/' + options.card_id + '/id/' + options.id;
    } else {
      wx.setNavigationBarTitle({
        title: '添加服务商品',
      })
      var url = 'https://www.58zltc.com/api.php/Card/getGoodAddForm/openid/' + app.globalData.openid +'/card_id/' + options.card_id;
    }
    app.ihttpGet(url, function (res) {
      that.setData({
        input_list: res.data.form,
        add_url: res.data.add_url,
        token: token,
        card_id: options.card_id
      })
    })
  },
  bindChange: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        list[idx].value = e.detail.value;
      }
    })
    this.setData({
      input_list: list
    })
  },
  bindSwitchChange: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        list[idx].checked = e.detail.value;
      }
    })
    this.setData({
      input_list: list
    })
  },
 
  formSubmit: function (e) {
    app.addFormId(e.detail.formId);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var post_data = e.detail.value;
    var data = this.data;
    let that = this;
    
    post_data.openid = app.globalData.openid;
    post_data.card_id = data.card_id;
    if (data.type == 'edit') {
      post_data.id = data.info.id;
    }
    app.ihttpPost(data.add_url, post_data, function (res) {
      if (res.data.err_code > 0) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else {
        wx.showToast({
          title: res.data.err_msg,
          icon: 'success'
        })
        setTimeout(function () {
          wx.navigateBack({})
        }, 500)
      }
    })
  },
})